﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Terraria.DataStructures;
using Terraria.GameContent.Tile_Entities;
using Terraria.ModLoader.Default;
using Terraria.ModLoader.Exceptions;

namespace Terraria.ModLoader.IO
{
	// Token: 0x020002DA RID: 730
	internal static class TileIO
	{
		// Token: 0x06001C19 RID: 7193 RVA: 0x004333A8 File Offset: 0x004315A8
		internal static TagCompound SaveTiles()
		{
			bool[] array = new bool[TileLoader.TileCount];
			bool[] array2 = new bool[WallLoader.WallCount];
			TagCompound result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
				{
					TileIO.WriteTileData(binaryWriter, array, array2);
					List<TagCompound> list = new List<TagCompound>();
					for (int i = 470; i < array.Length; i++)
					{
						if (array[i])
						{
							ModTile tile = TileLoader.GetTile(i);
							List<TagCompound> list2 = list;
							TagCompound tagCompound = new TagCompound();
							tagCompound["value"] = (short)i;
							tagCompound["mod"] = tile.mod.Name;
							tagCompound["name"] = tile.Name;
							tagCompound["framed"] = Main.tileFrameImportant[i];
							list2.Add(tagCompound);
						}
					}
					List<TagCompound> list3 = new List<TagCompound>();
					for (int j = 231; j < array2.Length; j++)
					{
						if (array2[j])
						{
							ModWall wall = WallLoader.GetWall(j);
							List<TagCompound> list4 = list3;
							TagCompound tagCompound2 = new TagCompound();
							tagCompound2["value"] = (short)j;
							tagCompound2["mod"] = wall.mod.Name;
							tagCompound2["name"] = wall.Name;
							list4.Add(tagCompound2);
						}
					}
					if (list.Count == 0 && list3.Count == 0)
					{
						result = null;
					}
					else
					{
						TagCompound tagCompound3 = new TagCompound();
						tagCompound3["tileMap"] = list;
						tagCompound3["wallMap"] = list3;
						tagCompound3["data"] = memoryStream.ToArray();
						result = tagCompound3;
					}
				}
			}
			return result;
		}

		// Token: 0x06001C1A RID: 7194 RVA: 0x00433578 File Offset: 0x00431778
		internal static void LoadTiles(TagCompound tag)
		{
			if (!tag.ContainsKey("data"))
			{
				return;
			}
			TileIO.TileTables tileTables = TileIO.TileTables.Create();
			foreach (TagCompound tagCompound in tag.GetList<TagCompound>("tileMap"))
			{
				ushort key = (ushort)tagCompound.GetShort("value");
				string @string = tagCompound.GetString("mod");
				string string2 = tagCompound.GetString("name");
				Mod mod = ModLoader.GetMod(@string);
				tileTables.tiles[key] = ((mod == null) ? 0 : ((ushort)mod.TileType(string2)));
				if (tileTables.tiles[key] == 0)
				{
					tileTables.tiles[key] = (ushort)ModLoader.GetMod("ModLoader").TileType("PendingMysteryTile");
					tileTables.tileModNames[key] = @string;
					tileTables.tileNames[key] = string2;
				}
				tileTables.frameImportant[key] = tagCompound.GetBool("framed");
			}
			foreach (TagCompound tagCompound2 in tag.GetList<TagCompound>("wallMap"))
			{
				ushort key2 = (ushort)tagCompound2.GetShort("value");
				string string3 = tagCompound2.GetString("mod");
				string string4 = tagCompound2.GetString("name");
				Mod mod2 = ModLoader.GetMod(string3);
				tileTables.walls[key2] = ((mod2 == null) ? 0 : ((ushort)mod2.WallType(string4)));
			}
			using (MemoryStream memoryStream = new MemoryStream(tag.GetByteArray("data")))
			{
				using (BinaryReader binaryReader = new BinaryReader(memoryStream))
				{
					TileIO.ReadTileData(binaryReader, tileTables);
				}
			}
		}

		// Token: 0x06001C1B RID: 7195 RVA: 0x00433774 File Offset: 0x00431974
		internal static void LoadLegacyTiles(BinaryReader reader)
		{
			TileIO.TileTables tileTables = TileIO.TileTables.Create();
			ushort num = reader.ReadUInt16();
			for (int i = 0; i < (int)num; i++)
			{
				ushort key = reader.ReadUInt16();
				string text = reader.ReadString();
				string text2 = reader.ReadString();
				Mod mod = ModLoader.GetMod(text);
				tileTables.tiles[key] = ((mod == null) ? 0 : ((ushort)mod.TileType(text2)));
				if (tileTables.tiles[key] == 0)
				{
					tileTables.tiles[key] = (ushort)ModLoader.GetMod("ModLoader").TileType("PendingMysteryTile");
					tileTables.tileModNames[key] = text;
					tileTables.tileNames[key] = text2;
				}
				tileTables.frameImportant[key] = reader.ReadBoolean();
			}
			num = reader.ReadUInt16();
			for (int j = 0; j < (int)num; j++)
			{
				ushort key2 = reader.ReadUInt16();
				string name = reader.ReadString();
				string name2 = reader.ReadString();
				Mod mod2 = ModLoader.GetMod(name);
				tileTables.walls[key2] = ((mod2 == null) ? 0 : ((ushort)mod2.WallType(name2)));
			}
			TileIO.ReadTileData(reader, tileTables);
		}

		// Token: 0x06001C1C RID: 7196 RVA: 0x00433898 File Offset: 0x00431A98
		internal static void WriteTileData(BinaryWriter writer, bool[] hasTile, bool[] hasWall)
		{
			byte b = 0;
			bool flag = false;
			int num = 0;
			int num2 = 0;
			do
			{
				Tile tile = Main.tile[num, num2];
				if (TileIO.HasModData(tile))
				{
					if (!flag)
					{
						writer.Write(b);
						b = 0;
					}
					else
					{
						flag = false;
					}
					TileIO.WriteModTile(ref num, ref num2, writer, ref flag, hasTile, hasWall);
				}
				else
				{
					b += 1;
					if (b == 255)
					{
						writer.Write(b);
						b = 0;
					}
				}
			}
			while (TileIO.NextTile(ref num, ref num2));
			if (b > 0)
			{
				writer.Write(b);
			}
		}

		// Token: 0x06001C1D RID: 7197 RVA: 0x00433910 File Offset: 0x00431B10
		internal static void ReadTileData(BinaryReader reader, TileIO.TileTables tables)
		{
			int num = 0;
			int num2 = 0;
			bool flag = false;
			for (;;)
			{
				if (!flag)
				{
					byte b;
					for (b = reader.ReadByte(); b == 255; b = reader.ReadByte())
					{
						for (byte b2 = 0; b2 < 255; b2 += 1)
						{
							if (!TileIO.NextTile(ref num, ref num2))
							{
								return;
							}
						}
					}
					for (byte b3 = 0; b3 < b; b3 += 1)
					{
						if (!TileIO.NextTile(ref num, ref num2))
						{
							return;
						}
					}
				}
				else
				{
					flag = false;
				}
				TileIO.ReadModTile(ref num, ref num2, tables, reader, ref flag);
				if (!TileIO.NextTile(ref num, ref num2))
				{
					return;
				}
			}
		}

		// Token: 0x06001C1E RID: 7198 RVA: 0x00433998 File Offset: 0x00431B98
		internal static void WriteModTile(ref int i, ref int j, BinaryWriter writer, ref bool nextModTile, bool[] hasTile, bool[] hasWall)
		{
			Tile tile = Main.tile[i, j];
			byte b = 0;
			byte[] array = new byte[11];
			int num = 1;
			if (tile.active() && tile.type >= 470)
			{
				hasTile[(int)tile.type] = true;
				b |= 1;
				array[num] = (byte)tile.type;
				num++;
				array[num] = (byte)(tile.type >> 8);
				num++;
				if (Main.tileFrameImportant[(int)tile.type])
				{
					array[num] = (byte)tile.frameX;
					num++;
					if (tile.frameX >= 256)
					{
						b |= 2;
						array[num] = (byte)(tile.frameX >> 8);
						num++;
					}
					array[num] = (byte)tile.frameY;
					num++;
					if (tile.frameY >= 256)
					{
						b |= 4;
						array[num] = (byte)(tile.frameY >> 8);
						num++;
					}
				}
				if (tile.color() != 0)
				{
					b |= 8;
					array[num] = tile.color();
					num++;
				}
			}
			if (tile.wall >= 231)
			{
				hasWall[(int)tile.wall] = true;
				b |= 16;
				array[num] = (byte)tile.wall;
				num++;
				array[num] = (byte)(tile.wall >> 8);
				num++;
				if (tile.wallColor() != 0)
				{
					b |= 32;
					array[num] = tile.wallColor();
					num++;
				}
			}
			int num2 = i;
			int num3 = j;
			byte b2 = 0;
			while (TileIO.NextTile(ref num2, ref num3))
			{
				if (tile.isTheSameAs(Main.tile[num2, num3]) && b2 < 255)
				{
					b2 += 1;
					i = num2;
					j = num3;
				}
				else
				{
					if (TileIO.HasModData(Main.tile[num2, num3]))
					{
						b |= 128;
						nextModTile = true;
						break;
					}
					break;
				}
			}
			if (b2 > 0)
			{
				b |= 64;
				array[num] = b2;
				num++;
			}
			array[0] = b;
			writer.Write(array, 0, num);
		}

		// Token: 0x06001C1F RID: 7199 RVA: 0x00433B74 File Offset: 0x00431D74
		internal static void ReadModTile(ref int i, ref int j, TileIO.TileTables tables, BinaryReader reader, ref bool nextModTile)
		{
			byte b = reader.ReadByte();
			Tile tile = Main.tile[i, j];
			if ((b & 1) == 1)
			{
				tile.active(true);
				ushort key = reader.ReadUInt16();
				tile.type = tables.tiles[key];
				if (tables.frameImportant[key])
				{
					if ((b & 2) == 2)
					{
						tile.frameX = reader.ReadInt16();
					}
					else
					{
						tile.frameX = (short)reader.ReadByte();
					}
					if ((b & 4) == 4)
					{
						tile.frameY = reader.ReadInt16();
					}
					else
					{
						tile.frameY = (short)reader.ReadByte();
					}
				}
				else
				{
					tile.frameX = -1;
					tile.frameY = -1;
				}
				if ((int)tile.type == ModLoader.GetMod("ModLoader").TileType("PendingMysteryTile") && tables.tileNames.ContainsKey(key))
				{
					MysteryTileInfo item;
					if (tables.frameImportant[key])
					{
						item = new MysteryTileInfo(tables.tileModNames[key], tables.tileNames[key], tile.frameX, tile.frameY);
					}
					else
					{
						item = new MysteryTileInfo(tables.tileModNames[key], tables.tileNames[key]);
					}
					MysteryTilesWorld mysteryTilesWorld = (MysteryTilesWorld)ModLoader.GetMod("ModLoader").GetModWorld("MysteryTilesWorld");
					int num = mysteryTilesWorld.pendingInfos.IndexOf(item);
					if (num < 0)
					{
						num = mysteryTilesWorld.pendingInfos.Count;
						mysteryTilesWorld.pendingInfos.Add(item);
					}
					MysteryTileFrame mysteryTileFrame = new MysteryTileFrame(num);
					tile.frameX = mysteryTileFrame.FrameX;
					tile.frameY = mysteryTileFrame.FrameY;
				}
				if ((b & 8) == 8)
				{
					tile.color(reader.ReadByte());
				}
				WorldGen.tileCounts[(int)tile.type] += (((double)j <= Main.worldSurface) ? 5 : 1);
			}
			if ((b & 16) == 16)
			{
				tile.wall = tables.walls[reader.ReadUInt16()];
				if ((b & 32) == 32)
				{
					tile.wallColor(reader.ReadByte());
				}
			}
			if ((b & 64) == 64)
			{
				byte b2 = reader.ReadByte();
				for (byte b3 = 0; b3 < b2; b3 += 1)
				{
					TileIO.NextTile(ref i, ref j);
					Main.tile[i, j].CopyFrom(tile);
					WorldGen.tileCounts[(int)tile.type] += (((double)j <= Main.worldSurface) ? 5 : 1);
				}
			}
			if ((b & 128) == 128)
			{
				nextModTile = true;
			}
		}

		// Token: 0x06001C20 RID: 7200 RVA: 0x00014219 File Offset: 0x00012419
		private static bool HasModData(Tile tile)
		{
			return (tile.active() && tile.type >= 470) || tile.wall >= 231;
		}

		// Token: 0x06001C21 RID: 7201 RVA: 0x00013F87 File Offset: 0x00012187
		private static bool NextTile(ref int i, ref int j)
		{
			j++;
			if (j >= Main.maxTilesY)
			{
				j = 0;
				i++;
				if (i >= Main.maxTilesX)
				{
					return false;
				}
			}
			return true;
		}

		// Token: 0x06001C22 RID: 7202 RVA: 0x00433DEC File Offset: 0x00431FEC
		internal static void VanillaSaveFrames(Tile tile, ref short frameX)
		{
			if (tile.type == 128 || tile.type == 269)
			{
				int slot = (int)(tile.frameX / 100);
				int position = (int)(tile.frameY / 18);
				if (TileIO.HasModArmor(slot, position))
				{
					frameX %= 100;
				}
			}
		}

		// Token: 0x06001C23 RID: 7203 RVA: 0x00433E38 File Offset: 0x00432038
		internal static TagCompound SaveContainers()
		{
			MemoryStream memoryStream = new MemoryStream();
			BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
			byte[] array = new byte[1];
			byte b = 0;
			ISet<int> set = new HashSet<int>();
			ISet<int> set2 = new HashSet<int>();
			ISet<int> set3 = new HashSet<int>();
			IDictionary<int, int> dictionary = new Dictionary<int, int>();
			for (int i = 0; i < Main.maxTilesX; i++)
			{
				for (int j = 0; j < Main.maxTilesY; j++)
				{
					Tile tile = Main.tile[i, j];
					if (tile.active() && (tile.type == 128 || tile.type == 269))
					{
						int num = (int)(tile.frameX / 100);
						int num2 = (int)(tile.frameY / 18);
						if (TileIO.HasModArmor(num, num2))
						{
							if (num2 == 0)
							{
								set.Add(num);
							}
							else if (num2 == 1)
							{
								set2.Add(num);
							}
							else if (num2 == 2)
							{
								set3.Add(num);
							}
							byte[] array2 = array;
							int num3 = 0;
							array2[num3] |= 1;
							b = 1;
						}
					}
				}
			}
			int num4 = 0;
			foreach (KeyValuePair<int, TileEntity> keyValuePair in TileEntity.ByID)
			{
				TEItemFrame teitemFrame = keyValuePair.Value as TEItemFrame;
				if (teitemFrame != null && ItemLoader.NeedsModSaving(teitemFrame.item))
				{
					dictionary.Add(teitemFrame.ID, num4);
					b = 1;
				}
				num4++;
			}
			if (b == 0)
			{
				return null;
			}
			binaryWriter.Write(b);
			binaryWriter.Write(array, 0, (int)b);
			if ((array[0] & 1) == 1)
			{
				binaryWriter.Write((ushort)set.Count);
				foreach (int num5 in set)
				{
					binaryWriter.Write((ushort)num5);
					ModItem item = ItemLoader.GetItem(EquipLoader.slotToId[EquipType.Head][num5]);
					binaryWriter.Write(item.mod.Name);
					binaryWriter.Write(item.Name);
				}
				binaryWriter.Write((ushort)set2.Count);
				foreach (int num6 in set2)
				{
					binaryWriter.Write((ushort)num6);
					ModItem item2 = ItemLoader.GetItem(EquipLoader.slotToId[EquipType.Body][num6]);
					binaryWriter.Write(item2.mod.Name);
					binaryWriter.Write(item2.Name);
				}
				binaryWriter.Write((ushort)set3.Count);
				foreach (int num7 in set3)
				{
					binaryWriter.Write((ushort)num7);
					ModItem item3 = ItemLoader.GetItem(EquipLoader.slotToId[EquipType.Legs][num7]);
					binaryWriter.Write(item3.mod.Name);
					binaryWriter.Write(item3.Name);
				}
				TileIO.WriteContainerData(binaryWriter);
			}
			TagCompound tagCompound = new TagCompound();
			tagCompound.Set("data", memoryStream.ToArray());
			if (dictionary.Count > 0)
			{
				tagCompound.Set("itemFrames", dictionary.Select(delegate(KeyValuePair<int, int> entry)
				{
					TagCompound tagCompound2 = new TagCompound();
					tagCompound2["id"] = entry.Value;
					tagCompound2["item"] = ItemIO.Save(((TEItemFrame)TileEntity.ByID[entry.Key]).item);
					return tagCompound2;
				}).ToList<TagCompound>());
			}
			return tagCompound;
		}

		// Token: 0x06001C24 RID: 7204 RVA: 0x004341D4 File Offset: 0x004323D4
		internal static void LoadContainers(TagCompound tag)
		{
			if (tag.ContainsKey("data"))
			{
				TileIO.ReadContainers(new BinaryReader(new MemoryStream(tag.GetByteArray("data"))));
			}
			foreach (TagCompound tagCompound in tag.GetList<TagCompound>("itemFrames"))
			{
				TEItemFrame teitemFrame = TileEntity.ByID[tag.GetInt("id")] as TEItemFrame;
				ItemIO.Load(teitemFrame.item, tagCompound.GetCompound("item"));
			}
		}

		// Token: 0x06001C25 RID: 7205 RVA: 0x00434278 File Offset: 0x00432478
		internal static void ReadContainers(BinaryReader reader)
		{
			byte[] array = new byte[1];
			reader.Read(array, 0, (int)reader.ReadByte());
			if ((array[0] & 1) == 1)
			{
				TileIO.ContainerTables containerTables = TileIO.ContainerTables.Create();
				int num = (int)reader.ReadUInt16();
				for (int i = 0; i < num; i++)
				{
					int key = (int)reader.ReadUInt16();
					string name = reader.ReadString();
					string name2 = reader.ReadString();
					Mod mod = ModLoader.GetMod(name);
					containerTables.headSlots[key] = ((mod != null) ? mod.GetItem(name2).item.headSlot : 0);
				}
				num = (int)reader.ReadUInt16();
				for (int j = 0; j < num; j++)
				{
					int key2 = (int)reader.ReadUInt16();
					string name3 = reader.ReadString();
					string name4 = reader.ReadString();
					Mod mod2 = ModLoader.GetMod(name3);
					containerTables.bodySlots[key2] = ((mod2 != null) ? mod2.GetItem(name4).item.bodySlot : 0);
				}
				num = (int)reader.ReadUInt16();
				for (int k = 0; k < num; k++)
				{
					int key3 = (int)reader.ReadUInt16();
					string name5 = reader.ReadString();
					string name6 = reader.ReadString();
					Mod mod3 = ModLoader.GetMod(name5);
					containerTables.legSlots[key3] = ((mod3 != null) ? mod3.GetItem(name6).item.legSlot : 0);
				}
				TileIO.ReadContainerData(reader, containerTables);
			}
			if ((array[0] & 2) == 2)
			{
				int num2 = reader.ReadInt32();
				for (int l = 0; l < num2; l++)
				{
					int key4 = reader.ReadInt32();
					TEItemFrame teitemFrame = TileEntity.ByID[key4] as TEItemFrame;
					ItemIO.LoadLegacy(teitemFrame.item, reader, true, false);
				}
			}
		}

		// Token: 0x06001C26 RID: 7206 RVA: 0x00434418 File Offset: 0x00432618
		internal static void WriteContainerData(BinaryWriter writer)
		{
			for (int i = 0; i < Main.maxTilesX; i++)
			{
				for (int j = 0; j < Main.maxTilesY; j++)
				{
					Tile tile = Main.tile[i, j];
					if (tile.active() && (tile.type == 128 || tile.type == 269))
					{
						int num = (int)(tile.frameX / 100);
						int num2 = (int)(tile.frameX % 100);
						int num3 = (int)(tile.frameY / 18);
						if (TileIO.HasModArmor(num, num3) && num2 % 36 == 0)
						{
							writer.Write(i);
							writer.Write(j);
							writer.Write((byte)num3);
							writer.Write((ushort)num);
						}
					}
				}
			}
			writer.Write(-1);
		}

		// Token: 0x06001C27 RID: 7207 RVA: 0x004344D8 File Offset: 0x004326D8
		internal static void ReadContainerData(BinaryReader reader, TileIO.ContainerTables tables)
		{
			for (int i = reader.ReadInt32(); i > 0; i = reader.ReadInt32())
			{
				int num = reader.ReadInt32();
				int num2 = (int)reader.ReadByte();
				int num3 = (int)reader.ReadUInt16();
				Tile tile = Main.tile[i, num];
				Tile tile2 = Main.tile[i + 1, num];
				if (tile.active() && tile2.active() && (tile.type == 128 || tile.type == 269) && tile.type == tile2.type && (tile.frameX == 0 || tile.frameX == 36) && tile2.frameX == tile.frameX + 18 && (int)(tile.frameY / 18) == num2 && tile.frameY == tile2.frameY)
				{
					if (num2 == 0)
					{
						num3 = tables.headSlots[num3];
					}
					else if (num2 == 1)
					{
						num3 = tables.bodySlots[num3];
					}
					else if (num2 == 2)
					{
						num3 = tables.legSlots[num3];
					}
					Tile tile3 = tile;
					tile3.frameX += (short)(100 * num3);
				}
			}
		}

		// Token: 0x06001C28 RID: 7208 RVA: 0x00014242 File Offset: 0x00012442
		private static bool HasModArmor(int slot, int position)
		{
			if (position == 0)
			{
				return slot >= 216;
			}
			if (position == 1)
			{
				return slot >= 210;
			}
			return position == 2 && slot >= 161;
		}

		// Token: 0x06001C29 RID: 7209 RVA: 0x0043460C File Offset: 0x0043280C
		internal static List<TagCompound> SaveTileEntities()
		{
			List<TagCompound> list = new List<TagCompound>();
			foreach (KeyValuePair<int, TileEntity> keyValuePair in TileEntity.ByID)
			{
				if (keyValuePair.Value.type >= 3)
				{
					ModTileEntity modTileEntity = (ModTileEntity)keyValuePair.Value;
					List<TagCompound> list2 = list;
					TagCompound tagCompound = new TagCompound();
					tagCompound["mod"] = modTileEntity.mod.Name;
					tagCompound["name"] = modTileEntity.Name;
					tagCompound["X"] = modTileEntity.Position.X;
					tagCompound["Y"] = modTileEntity.Position.Y;
					tagCompound["data"] = modTileEntity.Save();
					list2.Add(tagCompound);
				}
			}
			return list;
		}

		// Token: 0x06001C2A RID: 7210 RVA: 0x004346FC File Offset: 0x004328FC
		internal static void LoadTileEntities(IList<TagCompound> list)
		{
			foreach (TagCompound tagCompound in list)
			{
				Mod mod = ModLoader.GetMod(tagCompound.GetString("mod"));
				ModTileEntity modTileEntity = (mod != null) ? mod.GetTileEntity(tagCompound.GetString("name")) : null;
				if (modTileEntity == null)
				{
					goto IL_DE;
				}
				ModTileEntity modTileEntity2 = ModTileEntity.ConstructFromBase(modTileEntity);
				modTileEntity2.type = (byte)modTileEntity.Type;
				modTileEntity2.Position = new Point16(tagCompound.GetShort("X"), tagCompound.GetShort("Y"));
				if (tagCompound.ContainsKey("data"))
				{
					try
					{
						modTileEntity2.Load(tagCompound.GetCompound("data"));
						if (modTileEntity2 is MysteryTileEntity)
						{
							((MysteryTileEntity)modTileEntity2).TryRestore(ref modTileEntity2);
						}
						goto IL_138;
					}
					catch (Exception inner)
					{
						throw new CustomModDataException(mod, "Error in reading " + modTileEntity.Name + " tile entity data for " + mod.Name, inner);
					}
					goto IL_DE;
				}
				IL_138:
				if (modTileEntity.ValidTile((int)modTileEntity2.Position.X, (int)modTileEntity2.Position.Y))
				{
					modTileEntity2.ID = TileEntity.AssignNewID();
					TileEntity.ByID[modTileEntity2.ID] = modTileEntity2;
					TileEntity tileEntity;
					if (TileEntity.ByPosition.TryGetValue(modTileEntity2.Position, out tileEntity))
					{
						TileEntity.ByID.Remove(tileEntity.ID);
					}
					TileEntity.ByPosition[modTileEntity2.Position] = modTileEntity2;
					continue;
				}
				continue;
				IL_DE:
				modTileEntity = ModLoader.GetMod("ModLoader").GetTileEntity("MysteryTileEntity");
				modTileEntity2 = ModTileEntity.ConstructFromBase(modTileEntity);
				modTileEntity2.type = (byte)modTileEntity.Type;
				modTileEntity2.Position = new Point16(tagCompound.GetShort("X"), tagCompound.GetShort("Y"));
				((MysteryTileEntity)modTileEntity2).SetData(tagCompound);
				goto IL_138;
			}
		}

		// Token: 0x020002DB RID: 731
		internal struct TileTables
		{
			// Token: 0x06001C2B RID: 7211 RVA: 0x00434908 File Offset: 0x00432B08
			internal static TileIO.TileTables Create()
			{
				return new TileIO.TileTables
				{
					tiles = new Dictionary<ushort, ushort>(),
					frameImportant = new Dictionary<ushort, bool>(),
					walls = new Dictionary<ushort, ushort>(),
					tileModNames = new Dictionary<ushort, string>(),
					tileNames = new Dictionary<ushort, string>()
				};
			}

			// Token: 0x040018A7 RID: 6311
			internal IDictionary<ushort, ushort> tiles;

			// Token: 0x040018A8 RID: 6312
			internal IDictionary<ushort, bool> frameImportant;

			// Token: 0x040018A9 RID: 6313
			internal IDictionary<ushort, ushort> walls;

			// Token: 0x040018AA RID: 6314
			internal IDictionary<ushort, string> tileModNames;

			// Token: 0x040018AB RID: 6315
			internal IDictionary<ushort, string> tileNames;
		}

		// Token: 0x020002DC RID: 732
		internal struct ContainerTables
		{
			// Token: 0x06001C2C RID: 7212 RVA: 0x0043495C File Offset: 0x00432B5C
			internal static TileIO.ContainerTables Create()
			{
				return new TileIO.ContainerTables
				{
					headSlots = new Dictionary<int, int>(),
					bodySlots = new Dictionary<int, int>(),
					legSlots = new Dictionary<int, int>()
				};
			}

			// Token: 0x040018AC RID: 6316
			internal IDictionary<int, int> headSlots;

			// Token: 0x040018AD RID: 6317
			internal IDictionary<int, int> bodySlots;

			// Token: 0x040018AE RID: 6318
			internal IDictionary<int, int> legSlots;
		}
	}
}
